package com.xm9m.ibczy.mytest.alimama.mobile.demo;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.alimama.mobile.sdk.MmuSDK;
import com.alimama.mobile.sdk.config.InsertController;
import com.alimama.mobile.sdk.config.InsertController.InterstitialState;
import com.alimama.mobile.sdk.config.InsertController.OnStateChangeCallBackListener;
import com.alimama.mobile.sdk.config.InsertProperties;
import com.alimama.mobile.sdk.config.MmuController.ClickCallBackListener;
import com.alimama.mobile.sdk.config.MmuSDKFactory;
import com.xm9m.ibczy.mytest.R;

public class InsertActivity extends BaseActivity{
    private InsertProperties properties;
    private InsertController<?> mController;
	private MmuSDK mmuSDK; 
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.interstitial);
        setTitle("插屏");

        ViewGroup nat = (ViewGroup) findViewById(R.id.nat);
        final String slotId = "59338";//注意：该广告位只做测试使用，请勿集成到发布版app中
        setupAlimama(nat, slotId);

        Button btLoad = (Button) findViewById(R.id.interstitialLoad);
        btLoad.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(mController != null){
                    mController.load(slotId);
                }
            }
        });
        
        Button btClose = (Button) findViewById(R.id.interstitialClose);
        btClose.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                if(mController != null){
                    mController.close();
                }
            }
        });
    }

	private void setupAlimama(ViewGroup nat, String slotId) {
        mmuSDK = MmuSDKFactory.getMmuSDK();
        mmuSDK.init(getApplication());//初始化SDK,该方法必须保证在集成代码前调用，可移到程序入口处调用
        mmuSDK.accountServiceInit(this);
        properties = new InsertProperties(slotId, nat);
        mmuSDK.attach(properties);

        properties.setClickCallBackListener(new ClickCallBackListener() {

            @Override
            public void onClick() {
                Log.i("munion", "onclick");
            }
        });

        properties.setOnStateChangeCallBackListener(new OnStateChangeCallBackListener() {

            @Override
            public void onStateChanged(InterstitialState state) {
                Log.i("munion", "state = " + state);
            }
        });
        mController =  properties.getMmuController();
    }
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (!mmuSDK.accountServiceHandleResult(requestCode, resultCode, data,this)) {
			super.onActivityResult(requestCode, resultCode, data);
		}
	}
}
